﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microhard.ApproveWorkflow.Interfaces;
using Microhard.ApproveWorkflow.DTO;
using Microhard.ApproveWorkflow.Models;
using Microhard.Core.DBUtility;

namespace Microhard.ApproveWorkflow.Impls
{
    public class WorkflowApproveResultImpl : IWorkflowApproveResult
    {
        #region IRepository<WorkflowApproveResultModel> Members

        public ExecutedResult Add(Models.WorkflowApproveResultModel model)
        {
            ExecutedResult result = new ExecutedResult();
            //WorkflowDBDataContext db = new WorkflowDBDataContext();
            ////是否存在表中
            //var rtn = db.WorkflowDataApproveResults.Count(d => d.DataID == model.DataID);
            //if (rtn > 0)
            //{ 
            //     result.ErrorMsg.Add("WorkflowApproveResultImpl.Add","此流程已添加");
            //     return result;
            //}
            //var toAdd = model.ToEntity();
            //toAdd.ApproveID = Guid.NewGuid();
            //toAdd.CreatTime = DateTime.Now;
            //toAdd.LastModifiedTime = DateTime.Now;

            //db.WorkflowDataApproveResults.InsertOnSubmit(toAdd);

            //try
            //{
            //    db.SubmitChanges();
            //}
            //catch (Exception ex)
            //{
            //    result.ErrorMsg.Add("WorkflowApproveResultImpl.Add", ex.Message);
            //}
            return result;
        }

        public ExecutedResult Update(Models.WorkflowApproveResultModel model)
        {
            ExecutedResult result = new ExecutedResult();
            //WorkflowDBDataContext db = new WorkflowDBDataContext();
            //var toUpdate = db.WorkflowDataApproveResults.SingleOrDefault(b => b.DataID == model.DataID);
            //if (toUpdate != null)
            //{
            //    toUpdate.Status = (int)WorkflowResultStatus.Approve;
            //    try
            //    {
            //        db.SubmitChanges();
            //    }
            //    catch (Exception ex)
            //    {
            //        result.ErrorMsg.Add("WorkflowApproveResultImpl.Update", ex.Message);
            //    }
            //    return result;

            //}
            return result;
        }

        public ExecutedResult Delete(List<Guid> ids)
        {
            ExecutedResult result = new ExecutedResult();
            //WorkflowDBDataContext db = new WorkflowDBDataContext();
            //var toDeltes = from b in db.WorkflowDataApproveResults
            //               where ids.Contains(b.DataID)
            //               select b;
            //db.WorkflowDataApproveResults.DeleteAllOnSubmit(toDeltes);
            //try
            //{
            //    db.SubmitChanges();
            //}
            //catch (Exception ex)
            //{
            //    result.ErrorMsg.Add("WorkflowApproveResultImpl.Delete", ex.Message);
            //}
            return result;
        }

        public ExecutedResult Delete(Models.WorkflowApproveResultModel model)
        {
            throw new NotImplementedException();
        }

        public Models.WorkflowApproveResultModel GetModelByID(Guid id)
        {
            //WorkflowDBDataContext db = new WorkflowDBDataContext();
            //return db.WorkflowDataApproveResults.SingleOrDefault(d => d.DataID == id).ToModel();
            return null;
        }

        public Models.WorkflowApproveResultModel GetModelByModel(Models.WorkflowApproveResultModel model)
        {
            throw new NotImplementedException();
        }

        public List<Models.WorkflowApproveResultModel> Query(Models.WorkflowApproveResultModel Model, int pageIndex, int pageSize, out int totalCount)
        {
            throw new NotImplementedException();
        }

        public List<Models.WorkflowApproveResultModel> Query(Models.WorkflowApproveResultModel Model)
        {
            throw new NotImplementedException();
        }

        public ScopeResult scope
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        #endregion

        #region IWorkflowApproveResult Members

        public bool IsBusinessSheetCanDelOrUpdate(Guid dataID)
        {
            //WorkflowDBDataContext db = new WorkflowDBDataContext();
            //var rtn = db.WorkflowDataApproveResults.SingleOrDefault(d => d.DataID == dataID
            //   && d.Status == (int)WorkflowResultStatus.Approve);
            //if (rtn != null)
            //{
            //    if (rtn.Status == (int)WorkflowResultStatus.Approve)
            //        return false;
            //}
            return true;
        }

        #endregion
    }
}
